<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/12
 * Time: 下午13:08
 */

class ApiBannerController extends BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-12 13:09:10
     * @Description: 轮播图列表
     * @link: http://www.activity.com/wechat/api3.0/banner_list?token=83545d86fe43b7a89873596fbe56e80b&city_name=%E5%8D%97%E4%BA%AC%E5%B8%82&source=2
     */
    public function banner_list(){
        $validator = Validator::make(Input::all(),
            array(
                //'token' => 'required',
                'city_name' => 'required',
                'source' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
//            $mkt_user=$this->verificationUserToken(Input::get("token"),$response);
//            if(is_null($mkt_user)){
//                break;
//            }

            //调用判断城市接口
            $result = PublicApi::Judge_city(Input::get("city_name"));
            $city_id = $result;

            $whole = BannerCityWhole::where('city_id',$result)->where('status',1)->where('whole',1)->first();
            if(!is_null($whole)){
                //同全局
                $result = 0;
            }
            $banner_list = BannerRecordsWechat3::join("banner_city","banner_records_wechat3.id","=","banner_city.banner_id")
                ->where("banner_records_wechat3.status",1)
                ->where("city_id",$result)
                ->where("banner_city.source",Input::get('source'))
                ->where("banner_records_wechat3.start_time","<=",Common::currentTimeString())
                ->where("banner_records_wechat3.end_time",">=",Common::currentTimeString())
                ->select("banner_records_wechat3.id","banner_records_wechat3.banner_name","banner_records_wechat3.banner_img","banner_records_wechat3.target_url")
                ->orderBy("banner_city.position","asc")
                ->get()->toArray();
            if(!empty($banner_list)){
                foreach($banner_list as $_key => $_val){
                    $banner_list[$_key]['banner_img'] = Config::get('config.ahp_upload_files_url').$_val['banner_img'];
                    $banner_list[$_key]['url'] = Config::get('config.browse_url');
                }
            }

            $result = [
                'city_id' => $city_id,
                'city_name' => Input::get("city_name"),
                'banner' => $banner_list
            ];

            //如果城市开放，按照该城市查询，否则查询全局
//            if($result != 0){
//                //城市开放
//                //判断当前城市是否适配了全局轮播图配置
//                $banner_list = BannerRecords::join("banner_city","banner_records.id","=","banner_city.banner_id")
//                    ->where("banner_records.status",1)
//                    ->where("is_launch",1)
//                    ->where("city_id",$result)
//                    ->get();
//            }else{
//                //城市未开放
//                $banner_list = BannerRecords::join("banner_city","banner_records.id","=","banner_city.banner_id")
//                    ->where("banner_records.status",1)
//                    ->where("is_launch",1)
//                    ->where("city_id",0)
//                    ->get();
//            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "wechat/api3.0/banner_list",  'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


}